﻿<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>
<?php //check if GET set
	if (!isset($_GET['userid'])) {
		$to = "client.php";
		redirect_to($to);
		exit;  
	};
 ?>
<?php //check if GET set
	if (isset($_GET['nuobaudosid'])) {
		$nuobaudosid=($_GET['nuobaudosid']);
	}else{
		$nuobaudosid=0;
	};
 ?>

 <?php //check if svarstymas157id set
	if (!isset($_GET['svarstymas157id'])) {
		$svarstymas157id = 0;  
		}else{
		$svarstymas157id = ($_GET['svarstymas157id']);
		//gaunam paskatinimo detales
		$query = "SELECT * 
			FROM `svarstymas157`
			WHERE svarstymas157id=";
		$query .=($_GET['svarstymas157id']);
		$result_set = mysql_query($query, $connection);
		$sabotage = mysql_num_rows($result_set);
		$get157= mysql_fetch_array($result_set);
		//tikrinam ar yra toks paskatinimas ir ar jis priklauso tam klientui
		if(($sabotage==0) or (($get157['userid'])!=($_GET['userid']))){echo "<h1>Sabotage<h1>";};
		
		};
 ?>
 
<?php //check if user has primary photo

	$query = "SELECT *";
	$query .= " FROM `foto`";
	$query .= " WHERE `foto`.userid =";
	$query .= "'".$_GET['userid']."'";
	$query .= " AND `pagrindine` = 1";
	//echo $query."</br>";
	$result_set = mysql_query($query, $connection);
	$subject = mysql_fetch_array($result_set);
	
	if (($subject['pagrindine']) !=0) {
	$message = "Pagrindine foto jau yra";
	$pagridine_yra = 1;
	$foto_location = $subject['path'];
	$foto_id = $subject['fotoid'];
	
	} else {
	$message = "Pagrindines foto nera";
	$pagridine_yra = 0;
	};
	
	
?>

<?php //Gaunam Pagrindinius kliento duomenys
	
	$query = "SELECT *";
	$query .= " FROM `user`, `lovos`";
	$query .= " WHERE `user`.userid =";
	$query .= "'".$_GET['userid']."'";
	$query .= "AND `user`.lovosid = `lovos`.lovosid";
	
	//echo $query."</br>";
	$result_set = mysql_query($query, $connection);

	$subject = mysql_fetch_array($result_set);

?>

 <?php //procesinam ka gaunam is post
 
if (isset($_POST['submit'])) {
	
	if($svarstymas157id==0){
		//naujo pradzhia
		$sql = "INSERT INTO `svarstymas157` (`userid`, `svarstdata`, `charakterizuojamas`, `tarpininkauti`, `burys`";
		$sql .= ") VALUES (";
		$sql .= $subject['userid'] . ", ";
		$sql .= "'".$_POST['svarstymodata']."'" . ", ";
		$sql .= $_POST['charakterizuojamas'] . ", ";
		$sql .= $_POST['tarpininkavimas'] . ", ";
		$sql .= $subject['burys'];
		$sql .= ");";
		
		$result = mysql_query($sql, $connection);
			if ($result) {
			
	//===========================================================//
	//   HZ kur redirect
	//===========================================================//
			
			
				$to = "summary.php?userid=" . $_GET['userid'];
				redirect_to($to);
				// Success!
				// $message .= "</br>Client updated Successsullyy!! </br>";
			} else {
				// Display error message.
				$message .= "<p>Client update failed.</p>";
				$message .= "<p>" . mysql_error() . "</p>";
			};
		//naujo pabaiga
	}else{	
		//redagavimo pradzia
		$sql = "UPDATE `svarstymas157`";
		//$sql = "INSERT INTO `svarstymas157` (`userid`, `svarstdata`, `charakterizuojamas`, `tarpininkauti`, `burys`";
		//$sql .= ") VALUES (";
		$sql .= " SET";
		$sql .= " `userid`=";
		$sql .= $subject['userid'] . ", ";
		$sql .= " `svarstdata`=";
		$sql .= "'".$_POST['svarstymodata']."'" . ", ";
		$sql .= " `charakterizuojamas`=";
		$sql .= $_POST['charakterizuojamas'] . ", ";
		$sql .= " `tarpininkauti`=";
		$sql .= $_POST['tarpininkavimas'] . ", ";
		$sql .= " `burys`=";
		$sql .= $subject['burys'];
		$sql .= " WHERE `svarstymas157id`=";
		$sql .= $_GET['svarstymas157id'];
		
		$result = mysql_query($sql, $connection);
			if ($result) {
			
	//===========================================================//
	//   HZ kur redirect
	//===========================================================//
			
			
				$to = "summary.php?userid=" . $_GET['userid'];
				redirect_to($to);
				// Success!
				// $message .= "</br>Client updated Successsullyy!! </br>";
			} else {
				// Display error message.
				$message .= "<p>Client update failed.</p>";
				$message .= "<p>" . mysql_error() . "</p>";
			};
		//redagavimo pabaiga
	};	
};
	
  

 ?>



					<article>
						<h2><?php 
							if($svarstymas157id !=0){
								echo "Lygtinio redagavimas";
							}else{
								echo "Lygtinio formavimas";
							};
						
						?></h2>
	<?php echo $message."</br>";?>
	<?php //rodomi pagrindiniai kliento duomenys

	//print_r ($subject) ;
	
	//echo "Vardas: ";
	echo $subject['vardas'];
	echo ' ';
	
	// echo "Pavarde: ";
	echo $subject['pavarde'];
	echo ' ';	
	
	//echo "Bylos Nr.: ";
	echo $subject['bylosnr'];
	echo '</br>';
	$tau_dabar = $subject['tautybeid'];
	$pil_dabar = $subject['pilietybeid'];
	
?>
    <?php //rodom pagrindine foto jeigu yra
	if ($pagridine_yra == 1){ 
		echo "</br> <img src=\"";
		echo $foto_location;
		echo "\" width=\"150\">";
	};
?> 
   
<form action="form_157.php?userid=<?php echo ($_GET['userid']);if($svarstymas157id !=0){echo "&svarstymas157id=".$svarstymas157id;};?>" method="post" name="submit">
  
  </br>
  Svarstymo data: <input name="svarstymodata" type="date" value="<?php 
							if($svarstymas157id !=0){
								echo ($get157['svarstdata']);
							}else {echo date("Y-m-j");};
						
						?>" placeholder="1900-12-31" required/>
 <br>
  Charakterizuojamas: <select name="charakterizuojamas" size="1">
    <option value="3" <?php 
							if($svarstymas157id !=0){
								if(($get157['charakterizuojamas'])==3){echo "selected";};
							};
						
						?>>Teigimai</option>
    <option value="2" <?php 
							if($svarstymas157id !=0){
								if(($get157['charakterizuojamas'])==2){echo "selected";};
							};
						
						?>>Patenkinamai</option>
    <option value="1" <?php 
							if($svarstymas157id !=0){
								if(($get157['charakterizuojamas'])==1){echo "selected";};
							};
						
						?>>Tik patenkinamai</option>
    <option value="0" <?php 
							if($svarstymas157id !=0){
								if(($get157['charakterizuojamas'])==0){echo "selected";};
							};
						
						?>>Neigiamai</option>
			</select>
<br>
<p>
  <label>
    <input type="radio" name="tarpininkavimas" value="1" id="tarpininkavimas_0" <?php 
							if($svarstymas157id !=0){
								if(($get157['tarpininkauti'])==1){echo "checked";};
							};
						
						?>/>
    tarpininkauti</label>
  <br />
  <label>
    <input type="radio" name="tarpininkavimas" value="0" id="tarpininkavimas_1" <?php 
							if($svarstymas157id !=0){
								if(($get157['tarpininkauti'])==0){echo "checked";};
							};
						
						?>/>
    netarpininkauti</label>
  <br>
<input name="submit" type="submit" value="SubmiNt" />
</form>

			</article>
            
<?php require("includes/footer.php"); ?>
